var dbUtil = require("db-util")
module.exports = {
	trigger: {
		beforeCreate: async function(
			{
				collection,
				operation,
				where,
				field,
				addDataList
			} = {}) {
			for (var i = 0; i < addDataList.length; i++) {
				let d = addDataList[i]
				if (!d.taskId) {
					throw new Error("tasId参数错误")
				}
				// if (!Array.isArray(d.images) || !d.images.length) {
				// 	throw new Error("images参数错误")
				// }
				for (var j = 0; j < d.images.length; j++) {
					let url = d.images[j]
					let count = await dbUtil.getCount({
						collection: 't3ds-marketing-task-asset',
						where: {
							taskId: d.taskId,
							images: url
						}
					})
					if (count) {
						let name = '...' + url.substring(url.length - 15)
						throw new Error(`图片"${name}"已包含在其他素材里面，请更换！`)
					}
				}
			}
		}
	}
}